package com.zhangxiaofanfan.hotcode.second;


/**
 * TODO
 *
 * @author zhangxiaofanfan
 * @date 2024-05-28 15:22:42
 */
public class HotCode022 {
    public static void main(String[] args) {

    }

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
         if (headA == null || headB == null) {
             return null;
         }
         ListNode indexA = headA, indexB = headB;
         boolean[] newLayer = new boolean[2];
         while (indexA != null && indexB != null) {
             if (indexA == indexB) {
                 return indexA;
             }
             indexA = indexA.next;
             if (indexA == null && !newLayer[0]) {
                 indexA = headB;
                 newLayer[0] = true;
             }
             indexB = indexB.next;
             if (indexB == null && !newLayer[1]) {
                 indexB = headA;
                 newLayer[1] = true;
             }
         }
         return null;
    }

    static class ListNode {
        int val;
        ListNode next;
        ListNode(int x) {
            val = x;
            next = null;
        }
    }

}
